package com.xjj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjj.entity.*;
import com.xjj.excel.ExcelUtils;
import com.xjj.service.ManagerService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

/**
 * @author sxy
 * @version 1.0
 * @date 2022/10/20 14:49
 */
@RestController
@RequestMapping("/manage/admin/manager")
public class ManagerContorller {
    @Resource
    private ManagerService managerService;

    @DeleteMapping("/deleteManagerById/{id}")
    @ApiOperation(value = "通过id删除老师")
    public ResponseEntity deleteManagerById(@PathVariable int id){
        boolean flag = managerService.removeById(id);
        return new ResponseEntity<>(200,"ok",flag);
    }

    @ApiOperation(value = "查找所有老师所有信息")
    @GetMapping("/findAllManager")
    public ResponseEntity findAllManager(){
        List<Manager> managers = managerService.list();
        return new ResponseEntity<>(200,"ok",managers);
    }

    @ApiOperation(value = "根据账户查询账号")
    @PostMapping("/getByAccount/{account}")
    public ResponseEntity<Manager> getByAccount(@PathVariable("account") String account){
        Manager manager = managerService.getByAccount(account);
        return ResponseEntity.SUCCESS("ok",manager);
    }

    @PostMapping("/addManager")
    @ApiOperation(value = "添加老师（密码加密）")
    public ResponseEntity addManager(@RequestBody Manager manager){
        managerService.add(manager);
        return ResponseEntity.SUCCESS("ok",200);
    }

    @PostMapping("/import")
    @ApiOperation(value = "excel导入老师信息")
    public ResponseEntity importManager(@RequestPart("file") MultipartFile file)throws Exception{
        List<Manager> managers = ExcelUtils.readMultipartFile(file,Manager.class);
        for (Manager manager : managers) {
            
//          解决数字过大自动转化为科学计数法问题
            BigDecimal bigDecimal = new BigDecimal(manager.getTid());//创建BigDecimal对象，把科学计数转成数字
            String res = bigDecimal.toPlainString();//转成最终要的数字字符串
            manager.setTid(res);
            System.out.println(manager.getTid());

            managerService.add(manager);
        }
        return ResponseEntity.SUCCESS("导入成功",200);
    }

    @GetMapping("/getManagerById/{id}")
    @ApiOperation(value = "根据老师id查询老师信息")
    public ResponseEntity getManagerById(@PathVariable int id){
        Manager managerById = managerService.getManagerById(id);
        return new ResponseEntity(200,"ok",managerById);
    }



    @GetMapping("/getAllManager/{current}")
    @ApiOperation(value = "分页查询所有教师")
    public ResponseEntity getAllManager(@PathVariable("current") Integer current){
        if(current==0){
            Page<Manager> manager = managerService.getManager(1);
            return new ResponseEntity(200,"获取分页查询学校信息",manager);
        }else {
            Page<Manager> manager = managerService.getManager(current);
            return new ResponseEntity(200,"获取分页查询学校信息",manager);
        }
    }

    @PutMapping("/updateById")
    @ApiOperation(value = "通过id修改老师信息")
    public ResponseEntity updateById(@RequestBody Manager manager){
        managerService.updateById(manager);
        return ResponseEntity.SUCCESS("ok",200);
    }

    @GetMapping("getAllTeacherByKlassId/{klassId}")
    @ApiOperation(value = "根据班级id查询所有任课老师")
    public ResponseEntity getAllTeacherByKlassId(@PathVariable int klassId){
        List<Manager> managers = managerService.getAllTeacherByKlassId(klassId);
        return ResponseEntity.SUCCESS("ok",managers);
    } 

}
